import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/login'
import type { UserInfo } from '@/api/login'
export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    const userInfo = ref<UserInfo>({
      username: '',
      nickName: '',
      phoneNumber: ''
    })
    const isLogin = ref(false)

    function setToken(newToken: string) {
      token.value = newToken
    }

    function haveToken() {
      return !!token.value
    }

    function getInfo() {
      return getUserInfo().then(res => {
        isLogin.value = true
        userInfo.value = res.data
      })
    }
    return { token, isLogin, setToken, haveToken, getInfo }
  },
  {
    persist: {
      storage: sessionStorage,
      paths: ['token']
    }
  }
)
